<template>
  <!-- 顶部栏 -->
  <div class="header-container">
      <div>
          <!-- 使用 Font Awesome 图标作为 logo -->
          <a href="#" class="logo">
              <i class="fa-solid fa-paw"></i> My Pet
          </a>
      </div>
      <!-- 中间导航按钮 -->
      <div class="button-container">
          <button 
              id="beauty-btn"
              :class="{ 'active': currentRoute === '/pet-grooming' }"
              @click="navigateTo('/pet-grooming')"
          >
              宠物美容
          </button>
          <button 
              id="medical-btn"
              :class="{ 'active': currentRoute === '/pet-medical' }"
              @click="navigateTo('/pet-medical')"
          >
              宠物医疗
          </button>
          <button 
              id="training-btn"
              :class="{ 'active': currentRoute === '/pet-training' }"
              @click="navigateTo('/pet-training')"
          >
              宠物训练
          </button>
          <button 
              id="boarding-btn"
              :class="{ 'active': currentRoute === '/pet-boarding' }"
              @click="navigateTo('/pet-boarding')"
          >
              宠物寄养
          </button>
      </div>

      <div class="right-buttons">
          <div class="custom-select">
              <select name="pet_service" id="service" style="display: none">
                  <option value="service_appointment">服务预约</option>
                  <option value="service_order">服务订单</option>
                  <option value="service_review">服务评价</option>
              </select>
              <button class="select-button">宠物服务</button>
              <div class="custom-select-options">
                  <div class="custom-select-option" data-value="service_appointment">
                      服务预约
                  </div>
                  <div class="custom-select-option" data-value="service_order">
                      服务订单
                  </div>
                  <div class="custom-select-option" data-value="service_review">
                      服务评价
                  </div>
              </div>
          </div>
          <button @click="goToLogin">登陆</button>
          <button @click="goToRegister">注册</button>
          <!-- 圆形头像 -->
          <div class="avatar-button" @click="handleAvatarClick"></div>
      </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';

const router = useRouter();
const route = useRoute();
const currentRoute = ref(route.path);

const navigateTo = (path) => {
  router.push(path);
  currentRoute.value = path;
};

const goToLogin = () => {
  router.push('/login');
};

const goToRegister = () => {
  router.push('/register');
};

const goToReservation = () => {
  router.push('/reservation');
};

const handleAvatarClick = () => {
  // 这里可以添加头像点击后的逻辑，比如弹出个人中心等
  console.log('头像被点击');
};

const options = document.querySelectorAll(".custom-select-option");
const originalSelect = document.getElementById("service");

options.forEach((option) => {
  option.addEventListener("click", function () {
      originalSelect.value = this.getAttribute("data-value");
  });
});

onMounted(() => {
  const options = document.querySelectorAll('.custom-select-option');
  options.forEach(option => {
    if(option.getAttribute('data-value') === 'service_appointment') {
      option.addEventListener('click', goToReservation);
    }
  });
});
</script>

<style scoped>
/* 基础样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Font Awesome 6 Free', sans - serif;
}

/* 顶部导航栏样式 */
.header-container {
  background-color: #64A386;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 5%;
  height: 70px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.logo {
  color: #FBFCCD;
  font-size: 28px;
  font-weight: bold;
  text-decoration: none;
}

.button-container {
  display: flex;
  gap: 40px;
  align-items: center;
}

.button-container button {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  padding: 8px 0;
  position: relative;
  transition: all 0.3s ease;
}

.button-container button:hover {
  color: #FBFCCD;
}

.button-container button::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #FBFCCD;
  transition: width 0.3s ease;
}

.button-container button:hover::after {
  width: 100%;
}

.button-container button.active {
  color: #FBFCCD;
}

.button-container button.active::after {
  width: 100%;
}

.right-buttons {
  display: flex;
  gap: 15px;
  align-items: center;
}

.right-buttons button {
  background-color: #FBFCCD;
  border: none;
  color: black;
  cursor: pointer;
  padding: 8px 15px;
  border-radius: 5px;
  font-weight: 500;
  transition: background-color 0.3s ease;
}

.right-buttons button:hover {
  background-color: #BCDDBE;
}

/* 自定义下拉菜单 */
.custom-select {
  position: relative;
  display: inline-block;
}

.select-button {
  border: none;
  color: black;
  cursor: pointer;
  padding: 8px 30px 8px 15px;
  border-radius: 5px;
  font-weight: 500;
  transition: background-color 0.3s ease;
  position: relative;
  width: 100px;
  display: flex;
  align-items: center; /* 垂直居中对齐 */
  justify-content: space-between;
}

.select-button::after {
  content: "▼";
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 12px;
}

.custom-select-options {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: white;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
  border-radius: 5px;
  overflow: hidden;
}

.custom-select:hover .custom-select-options {
  display: block;
}

.custom-select-option {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.custom-select-option:hover {
  background-color: #BCDDBE;
}

/* 圆形头像按钮 */
.avatar-button {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: white;
  border: 2px solid white;
  background-image: url("https://via.placeholder.com/40");
  background-size: cover;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.avatar-button:hover {
  transform: scale(1.1);
}
</style>